using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace LocalBoard.Web
{
	/// <summary>
	/// Summary description for PreviewClubForm.
	/// </summary>
	public partial class PreviewClubForm : System.Web.UI.Page
	{
	
		protected string xmlForm = "";
		private DataTable XmlDataTable;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		protected void Page_Init(object sender, System.EventArgs e)
		{
			try
			{
				//	xmlForm = ConfigurationSettings.AppSettings["defaultForm"];
				xmlForm = getFormName();
				DataSet XmlDataSet = new DataSet();
				XmlDataSet.ReadXml(HttpContext.Current.Server.MapPath("Forms/" + xmlForm),
					XmlReadMode.InferSchema);

				XmlDataTable = new DataTable();
				XmlDataTable = XmlDataSet.Tables["Fields"];
				if(validFormTable(XmlDataTable))
				{
					loadForm();
				}
				else
				{
					formHolder.Controls.Add(new LiteralControl("<br><b>Can not load form. Please check the error below.</b><br>"));
					formHolder.Controls.Add(new LiteralControl("Control id must be unique and can not contain a white space."));
				}
			}
			catch
			{
				formHolder.Controls.Add(new LiteralControl("No club-join form found."));
			}
		}

		private string getFormName()
		{
			if(Request.Params["formName"] != null)
				return Request.Params["formName"];
			return "";
		}
		private bool validFormTable(DataTable formDataTable)
		{
			for(int i = 0;i < formDataTable.Rows.Count;i++)
			{
				string controlId = "controlId"+i.ToString();
				int j = i + 1;
				for(; j < formDataTable.Rows.Count; j++)
				{
					string controlIdToCompare = "controlId"+j.ToString();
					if(controlId.ToUpper() == controlIdToCompare.ToUpper())
						return false;
				}
			}
			return true;
		}

		private void loadForm()
		{
			formHolder.Controls.Add(new LiteralControl("<table><tr>"));
			//subject control
			TextBox subjectBox = new TextBox();
			subjectBox.ID = "subject";

			formHolder.Controls.Add(new LiteralControl("<td>Subject</td>"));
			formHolder.Controls.Add(new LiteralControl("<td>"));
			formHolder.Controls.Add(subjectBox);
			formHolder.Controls.Add(new LiteralControl("</td>"));
			formHolder.Controls.Add(new LiteralControl("<tr>"));
			for(int i = 0;i < XmlDataTable.Rows.Count;i++)
			{
				string objId = "controlId" + i.ToString();
				string objLabel = XmlDataTable.Rows[i]["Label"].ToString();
				bool required = XmlDataTable.Rows[i]["Required"].ToString().ToUpper() == "YES";
				if(XmlDataTable.Rows[i]["Control"].ToString() == "CheckBox")
				{
					CheckBox cBox = new CheckBox();
					cBox.ID = objId;
					cBox.Text = objLabel;
					cBox.TextAlign = TextAlign.Left;
					formHolder.Controls.Add(new LiteralControl("<td colspan='2'>"));
					formHolder.Controls.Add(cBox);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "TextBox")
				{
					Label label = new Label();
					label.Text = objLabel;
					TextBox tBox = new TextBox();
					tBox.ID = objId;

					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(tBox);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "CheckBoxList")
				{
					Label label = new Label();
					label.Text = objLabel;
					CheckBoxList cbList = new CheckBoxList();
					cbList.ID = objId;
					string[] cbItems = XmlDataTable.Rows[i]["Text"].ToString().Split(',');
					for (int j=0; j < cbItems.Length; j++)
					{
						ListItem item = new ListItem(cbItems[j], j.ToString());
						cbList.Items.Add(item);
					}
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(cbList);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "DropDownList")
				{
					Label label = new Label();
					label.Text = objLabel;
					DropDownList ddList = new DropDownList();
					ddList.ID = objId;
					string[] ddItems = XmlDataTable.Rows[i]["Text"].ToString().Split(',');
					for (int j=0; j < ddItems.Length; j++)
					{
						ListItem item = new ListItem(ddItems[j], j.ToString());
						ddList.Items.Add(item);
					}
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(ddList);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "Label")
				{
					Label label = new Label();
					label.Text = objLabel;
					label.ID = objId;
					formHolder.Controls.Add(new LiteralControl("<td colspan='2'>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "ListBox")
				{
					Label label = new Label();
					label.Text = objLabel;
					ListBox lBox = new ListBox();
					lBox.ID = objId;
					string[] lBoxItems = XmlDataTable.Rows[i]["Text"].ToString().Split(',');
					for (int j=0; j < lBoxItems.Length; j++)
					{
						ListItem item = new ListItem(lBoxItems[j], j.ToString());
						lBox.Items.Add(item);
					}
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(lBox);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "Multiline")
				{
					Label label = new Label();
					label.Text = objLabel;
					TextBox tBox = new TextBox();
					tBox.TextMode = TextBoxMode.MultiLine;
					tBox.Width = Unit.Pixel(200);
					tBox.Rows = 10;
					tBox.ID = objId;
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(tBox);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				else if(XmlDataTable.Rows[i]["Control"].ToString() == "RadioButtonList")
				{
					Label label = new Label();
					label.Text = objLabel;
					RadioButtonList rbList = new RadioButtonList();
					rbList.ID = objId;
					string[] lbListItems = XmlDataTable.Rows[i]["Text"].ToString().Split(',');
					for (int j=0; j < lbListItems.Length; j++)
					{
						ListItem item = new ListItem(lbListItems[j], j.ToString());
						rbList.Items.Add(item);
					}
					rbList.TextAlign = TextAlign.Right;
					rbList.RepeatDirection = RepeatDirection.Vertical;
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(label);
					formHolder.Controls.Add(new LiteralControl("</td>"));
					formHolder.Controls.Add(new LiteralControl("<td>"));
					formHolder.Controls.Add(rbList);
					formHolder.Controls.Add(new LiteralControl("</td>"));
				}
				formHolder.Controls.Add(new LiteralControl("</tr>"));
			}
			formHolder.Controls.Add(new LiteralControl("</table>"));
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}
